﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by:     Charles Turiño
////Date created:   July 17, 2012
////Modified by: 
////Date modified: 
////Reason: 
////ULTI-75099 Echo Conversion - PA Tax Collection Districts
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.PATaxCollectionDistrict
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Charles Turino", "Charles_Turino@ultimatesoftware.com")]
    [Category("PA Tax Collection Districts")]
    public class PATaxCollectionDistrict : MbUnitAutomatedTest
    {
        UltiPro UltiPro;
        UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("TaxCollectionDistricts");
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit();
        }

        [SetUp]
        public void SetUp()
        {
            UltiPro.Pages.Login.LoginAs("MerrillG", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
        }

        [TearDown]
        public void TearDown()
        {
            UltiPro.Macros.Logout.Of_UltiPro();
            Database.Company.RollBackDatabase();
        }

        /* This test will validate the functionality of the PA Tax Collection Districts Page
         */
        [Test]
        public void PATCDFunctionality()
        {
            NavigateToTCD();

            ResetCancelOnAdd();

            SaveOnAdd();

            PrintTCDPage();

            DeleteTCDPage();
        }

        /* This test will validate the required fields
         * */
        [Test]
        public void PATCDRequiredFields()
        {
            NavigateToTCD();

            EffectiveDateRequired();

            PATaxCollectionDistrictRequired();

            TaxGroupRequired();
         }

        /* This test will validate Effective Date Format
         **/
        [Test]
        public void EffectiveDateValidation()
        {
            NavigateToTCD();

            // Invalid Characters
            EffectiveDateFormat("~!@#$%^&*()_+", false); // Dont need to test for invalid date because content will be filtered out on its own

            // Alphabet
            EffectiveDateFormat("Todays date", false); // Dont need to test for invalid date because content will be filtered out on its own

            // Invalid Format - '13/01/YYYY
            string tempDate = "13/1/" + DateTime.Now.ToString("yyyy").ToString();
            EffectiveDateFormat(tempDate, true);

            // Invalid Format - 'MM/32/YYYY
            tempDate = DateTime.Now.ToString("MM").ToString() + "/32/" + DateTime.Now.ToString("yyyy").ToString();
            EffectiveDateFormat(tempDate, true);

            // Invalid Format - 'MM/32/2222 (Future year)
            tempDate = DateTime.Now.ToString("MM").ToString() + "/1/2222";
            EffectiveDateFormat(tempDate, true);

            // Invalid Format - 'DD/MM/YYYY
            tempDate = "13/" + DateTime.Now.ToString("MM").ToString() + "/" + DateTime.Now.ToString("yyyy").ToString();
            EffectiveDateFormat(tempDate, true);

            // Invalid Format - 'Next year
            tempDate = DateTime.Now.ToString("MM").ToString() + "/1/" + DateTime.Now.ToString("yyyy") + 1;
            EffectiveDateFormat(tempDate, true);
        }

        #region Helper Methods
        #region Functionality
        private void NavigateToTCD()
        {
            UltiPro.Menu.SystemConfiguration.Click(x => x.PATaxCollectionDistricts);
        }

        // This method sets the form. It was abstracted for reuse.
        private void setForm(string tcd, string taxGP, string eDate)
        {
            // Set form
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Set(x => x.DistrictDropdown, tcd);
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.TaxGroupOptionMover.MoveRight(taxGP);
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Set(x => x.EffectiveDateValue, eDate);
        }

        // This method will add a TCD and then click the reset button to verify that all the data on the form is reset to its default values.
        private void ResetCancelOnAdd()
        {
            // Get the first dat of the month
            string tempDate = DateTime.Now.ToString("MM").ToString() + "/1/" + DateTime.Now.ToString("yyyy").ToString();

            // Set form
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.Click(x => x.Add);
            setForm("LANCASTER", "PRTAX", tempDate);
          
            // Click Reset
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Reset);

            // Validate reset worked
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Is(x => x.DistrictDropdown, "innerHTML", "ADAMS TAX COLLECTION DISTRICT"); // Adams is the default
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Is(x => x.EffectiveDateValue, "innerHTML", "");

            // There is currently no way to validate that the destination mover box is empty.  Per Adam, fix will be checked in this week.
            // UltiPro.Pages.AddChangePaTaxCollectionDistrict.TaxGroupOptionMover.HasOption(x => x.Destination, "");  

            // Click Cancel
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Cancel);
            UltiPro.Alert.Accept();

            // Verify cancel worked by validating that the add button is on the page
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Exists(x => x.Add);
        }
        
        // This method validates that a TCD can be saved
        private void SaveOnAdd()
        {
            // Get the first dat of the month
            string tempDate = DateTime.Now.ToString("MM").ToString() + "/1/" + DateTime.Now.ToString("yyyy").ToString();

            // Set form
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.Click(x => x.Add);
            setForm("LANCASTER", "PRTAX", tempDate);

            // Click Save
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Save);
            
            // Validate that the TCD saved
            // *** TaxCollectionDistrictGrid has not been implemented - Eduardo might implement it
            // UltiPro.Pages.PaTaxCollectionDistrictsSummary.TaxCollectionDistrictsGrid. 
        }
       
        // This method will validate that the user can print the PA Tax Collection Districts
        private void PrintTCDPage()
        {
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.Click(x => x.Print);
            UltiPro.Browser.SwitchToWindow("Print Preview");
            UltiPro.Pages.PrintPreview.Click(x => x.CloseButton);
            UltiPro.Browser.SwitchToMainWindow();
        }

        private void DeleteTCDPage()
        {
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.NotExists(x => x.Delete);
        }
        #endregion
        #region Require Fields
        
        // This test will validate that the effective date is a required field
        private void EffectiveDateRequired()
        {
            // Set form
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.Click(x => x.Add);
            setForm("LANCASTER", "PRTAX", "");

            // Click Save
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Save);
            
            // Validate error message
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ErrorMessages.Has("Please correct the 1 highlighted field(s) before continuing");
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Reset);
         }

        // This test will validate that the TCD is a required field
        private void PATaxCollectionDistrictRequired()
        {
           /* The tax collection district is a drop down field with a default value so it will always have a value
            * This testcase is not a valid one because there is no way to validate if it is a required field
            * */
        }

        // This test will validate that the Tax Group is a required field
        private void TaxGroupRequired()
        {
            // Get the first dat of the month
            string tempDate = DateTime.Now.ToString("MM").ToString() + "/1/" + DateTime.Now.ToString("yyyy").ToString();

            // Set form
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Set(x => x.DistrictDropdown, "LANCASTER");
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.Set(x => x.EffectiveDateValue, tempDate);
       
            // Click Save
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Save);

            // Validate error message
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ErrorMessages.Has("Please correct the 1 highlighted field(s) before continuing");
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Cancel);
            UltiPro.Alert.Accept();
        }
        #endregion
        #region Effective Date Format

        // This method will test the effective date format
        private void EffectiveDateFormat(string effectiveDate, bool invalidDate)
        {
            // Set form
            UltiPro.Pages.PaTaxCollectionDistrictsSummary.ButtonBar.Click(x => x.Add);
            setForm("LANCASTER", "PRTAX", effectiveDate);
       
            // Click Save
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Save);

            // Validate error message
            if (invalidDate)
                UltiPro.Pages.AddChangePaTaxCollectionDistrict.ErrorMessages.Has("Invalid date entered.");
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ErrorMessages.Has("Please correct the 1 highlighted field(s) before continuing");
            UltiPro.Pages.AddChangePaTaxCollectionDistrict.ButtonBar.Click(x => x.Cancel);
            UltiPro.Alert.Accept(); 
        }

        #endregion
        #endregion
    }
}
